﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.zoo.ui {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.virals.*;
    import flash.text.*;
    import Display.aswingui.*;

    public class ZooSpeechBubble extends Sprite {

        private var m_win:JWindow;
        private var m_container:JPanel;
        private var m_body:JPanel;
        private var m_mainTxtContainer:JPanel;
        private var m_placeContainer:JPanel;
        private var m_btnContainer:JPanel;
        private var m_mainTxt:AssetPane;
        private var m_unlockTxt:JLabel;
        private var m_placeNowModule:ZooPlaceFromInventoryModule;
        private var m_btnAskForAnimals:CustomButton;
        private var m_defaultMessage:String;
        private var m_itemNameToPlace:String;
        private var m_enclosureToAskForAnimals:ZooEnclosure;
        private var m_askAgainTimer:Timer;

        public function ZooSpeechBubble(){
            this.m_mainTxtContainer = ASwingHelper.makeSoftBoxJPanelVertical();
            this.m_placeContainer = ASwingHelper.makeSoftBoxJPanelVertical();
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel();
            _local1.appendAll(this.m_mainTxtContainer, this.m_unlockTxt, this.m_placeContainer);
            this.m_unlockTxt = ASwingHelper.makeLabel("", EmbeddedArt.DEFAULT_FONT_NAME, 14, EmbeddedArt.lightGrayTextColor);
            this.m_btnContainer = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            this.m_body = ASwingHelper.makeSoftBoxJPanelVertical();
            this.m_body.appendAll(_local1, this.m_unlockTxt, this.m_btnContainer);
            ASwingHelper.setEasyBorder(this.m_body, 10, 10, 30, 10);
            var _local2:MarginBackground = new MarginBackground((new (ZooDialog.assetDict["speech_bubble"])() as DisplayObject), new Insets(0, 0, 0, 0));
            this.m_container = ASwingHelper.makeSoftBoxJPanel();
            this.m_container.setBackgroundDecorator(_local2);
            this.m_container.append(this.m_body);
            this.m_win = new JWindow(this);
            this.m_win.setContentPane(this.m_container);
            this.m_win.show();
            ASwingHelper.prepare(this.m_win);
        }
        public function setDefaultMessage(_arg1:String, _arg2:String=null, _arg3:ZooEnclosure=null, _arg4:Boolean=true):void{
            this.m_defaultMessage = _arg1;
            this.m_itemNameToPlace = _arg2;
            this.m_enclosureToAskForAnimals = _arg3;
            if (this.m_itemNameToPlace){
                this.m_placeNowModule = new ZooPlaceFromInventoryModule(this.m_itemNameToPlace);
                this.m_placeNowModule.addEventListener(Event.CLOSE, this.onPlaceItemModuleClose, false, 0, true);
            } else {
                this.m_placeNowModule = null;
            };
            if (this.m_enclosureToAskForAnimals){
                this.m_btnAskForAnimals = new CustomButton(ZLoc.t("Dialogs", "ZooDialog_askDonation"), null, "GreenButtonUI");
                this.m_btnAskForAnimals.addActionListener(this.onBtnAskForAnimalsClick, 0, true);
                this.m_btnContainer.append(this.m_btnAskForAnimals);
            } else {
                this.m_btnAskForAnimals = null;
            };
            if (_arg4){
                this.revertToDefault();
            };
        }
        public function setMessage(_arg1:String):void{
            this.m_placeContainer.removeAll();
            this.m_btnContainer.removeAll();
            this.m_mainTxtContainer.removeAll();
            this.endAskAgainPoll();
            this.m_mainTxt = ASwingHelper.makeMultilineText(_arg1, 300, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 16, EmbeddedArt.brownTextColor);
            this.m_mainTxtContainer.append(this.m_mainTxt);
            if (this.m_btnAskForAnimals){
                this.m_btnContainer.append(this.m_btnAskForAnimals);
            };
            ASwingHelper.prepare(this.m_win);
        }
        public function revertToDefault():void{
            this.setMessage(this.m_defaultMessage);
            if (this.m_placeNowModule){
                this.m_placeContainer.append(this.m_placeNowModule);
            };
            this.beginAskAgainPoll();
            ASwingHelper.prepare(this.m_win);
        }
        private function beginAskAgainPoll():void{
            this.updateAskAgainTimer();
            if (!this.m_askAgainTimer){
                this.m_askAgainTimer = new Timer(500);
                this.m_askAgainTimer.addEventListener(TimerEvent.TIMER, this.onPollTick);
            };
            this.m_askAgainTimer.start();
        }
        private function endAskAgainPoll():void{
            if (this.m_askAgainTimer){
                this.m_askAgainTimer.stop();
            };
        }
        private function onPollTick(_arg1:TimerEvent):void{
            this.updateAskAgainTimer();
        }
        private function updateAskAgainTimer():void{
            var _local1:Viral;
            var _local2:int;
            var _local3:String;
            if (this.m_btnAskForAnimals){
                _local1 = Global.world.viralMgr.getActiveViralByType(this.m_enclosureToAskForAnimals.getItem().feed);
                _local2 = (_local1) ? _local1.timeToUnlock() : 0;
                _local3 = GameUtil.formatMinutesSeconds(_local2);
                if (_local2 > 0){
                    this.m_unlockTxt.setText(ZLoc.t("Dialogs", "ZooDialog_askAgainIn", {time:_local3}));
                    this.m_btnAskForAnimals.setEnabled(false);
                } else {
                    this.m_unlockTxt.setText("");
                    this.m_btnAskForAnimals.setEnabled(true);
                };
            };
            ASwingHelper.prepare(this.m_win);
        }
        private function onBtnAskForAnimalsClick(_arg1:Event):void{
            StatsManager.sample(100, "Zoo", "onAnimalAsk", ZooDialogView.currentInstance.lastDefaultKey);
            Global.world.viralMgr.sendZooFeed(this.m_enclosureToAskForAnimals.getItem().name, Global.player.cityName);
            this.m_enclosureToAskForAnimals.updateStagePickEffect();
            this.beginAskAgainPoll();
        }
        private function onPlaceItemModuleClose(_arg1:Event):void{
            dispatchEvent(new Event(Event.CLOSE));
        }

    }
}//package Modules.zoo.ui 
